package beedoolgi;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.lang.management.ManagementFactory;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;


public class TimeRecord {

	@SuppressWarnings("unused")
	public static void main(String[] args) throws Exception {
		System.out.println(ManagementFactory.getRuntimeMXBean().getName());
		System.out.println("# of available processors: " + Runtime.getRuntime().availableProcessors());
		System.out.println("코드 구현 내용: parallel. initialise hiding. non-regex.");
		
		// wait for the start of profiling
		InputStreamReader r = new InputStreamReader(System.in);
        BufferedReader b = new BufferedReader(r);
        String userInput = b.readLine();
		// TODO Auto-generated method stub

	    // time check variable declaration
	    long start, end;
	    
	    // path to base directory
		String basePath = "D:/webpage_download/top100sites";
		// path to some of the top sites
	    String[] topSitesPath = { "/www.facebook.com/index.html",
	                "/www.google.com/index.html",
	                "/www.amazon.com/index.html",
	                "/www.imdb.com/index.html",
	                "/stackoverflow.com/index.html",
	                "/www.youtube.com/index.html"};
	    // path to partial files - Google
	    String[] googleInputPath = { "/www.google.com/partial1.html",
	                  "/www.google.com/partial2.html",
	                  "/www.google.com/partial3.html" };
	    // path to partial files - Youtube
	    String[] youtubeInputPath = { "/www.youtube.com/partial1.html",
	                      "/www.youtube.com/partial2.html",
	                      "/www.youtube.com/partial3.html",
	                      "/www.youtube.com/partial4.html",
	                      "/www.youtube.com/partial5.html",
	                      "/www.youtube.com/partial6.html" };
	    
	    start = System.currentTimeMillis();
	    // top sites File to String
	    String[] topSitesString = new String[topSitesPath.length];
	    for (int i = 0; i < topSitesPath.length; i++) {
	    	topSitesString[i] = new String(Files.readAllBytes(Paths.get(basePath + topSitesPath[i])));
	    }
	    // partial files File to String - Google
	    String[] googleInputString = new String[googleInputPath.length];
	    for (int i = 0; i < googleInputPath.length; i++) {
	    	googleInputString[i] = new String(Files.readAllBytes(Paths.get(basePath + googleInputPath[i])));
	    }
	    // partial files File to String - Youtube
	    String[] youtubeInputString = new String[youtubeInputPath.length];
	    for (int i = 0; i < youtubeInputPath.length; i++) {
	    	youtubeInputString[i] = new String(Files.readAllBytes(Paths.get(basePath + youtubeInputPath[i])));
	    }
	    end = System.currentTimeMillis();
	    System.out.println("File to String converting time is " + (end - start) + "ms");
	    System.out.println("=========================================================");
	    
	    // parsing result
	    Document doc = new Document("");
	    
	    int counter = 0;
	    long executionTime = 0;
	    int sum = 0;
	    
	    String exampleFilePath = "C:/Users/JH/workspace/test/example.txt";
	    String exampleFileString = new String(Files.readAllBytes(Paths.get(exampleFilePath)));
	    String externalPath = "C:/Users/JH/workspace/test/external.txt";
	    String externalString = new String(Files.readAllBytes(Paths.get(externalPath)));
	    String parsingUnitPath = "C:/Users/JH/workspace/test/example_parsing_unit.txt";
	    String parsingUnitString = new String(Files.readAllBytes(Paths.get(parsingUnitPath)));
	    
		start = System.currentTimeMillis();
//	    doc = Jsoup.parse(topSitesString[3], "UTF-8");
//	    doc = Jsoup.parse(topSitesString[5], "UTF-8");
//	    doc = Jsoup.parseParallel(topSitesString[3], "UTF-8", 0);
//	    doc = Jsoup.parseParallel(topSitesString[5], "UTF-8", 0);
		
		// initialize hiding test
		// 0: normal parse, 1: parallel parse, 2: 파싱할 스트링도 아예 넘겨서 initialise에 미치는 영향 볼 거.
//		printTime(externalString, parsingUnitString, 2, 2000);
	    userInput = b.readLine();
		
		printTime(externalString, parsingUnitString, 2, 0); // after warm-up sampling
		
	    System.out.println("terminated");
	}
	
	static void printTime(String input, String unit, int type, int iteration) throws Exception {
		if (type == 0) {
			// warm-up
			for (int i=0; i < iteration; i++) {
				Jsoup.parse(input, "UTF-8");
				System.out.println("iteration #: " + i);
			}
			long start = System.currentTimeMillis();
			Document doc = Jsoup.parse(input, "UTF-8");
			long end = System.currentTimeMillis();
			
			long executionTime = (end - start);
			System.out.println("Jsoup.parse execution time: " + executionTime);
		} else if (type == 1) {
			// warm-up
			for (int i=0; i < iteration; i++) {
				Jsoup.parseParallel(input, "UTF-8", 0);
				System.out.println("iteration #: " + i);
			}
			long start = System.currentTimeMillis();
			Document doc = Jsoup.parseParallel(input, "UTF-8", 0);
			long end = System.currentTimeMillis();
			
			long executionTime = (end - start);
			System.out.println("Jsoup.parseParallel execution time: " + executionTime);
		} else if (type == 2) {
			// warm-up
			for (int i=0; i < iteration; i++) {
				Jsoup.parseParallel_(input, unit, "UTF-8", 0);
				System.out.println("iteration #: " + i);
			}
			long start = System.currentTimeMillis();
			Document doc = Jsoup.parseParallel_(input, unit, "UTF-8", 0);
			long end = System.currentTimeMillis();
			
			long executionTime = (end - start);
			System.out.println("Jsoup.parseParallel execution time: " + executionTime);
		}
	}
}
